home *** CD-ROM | disk | FTP | other *** search
- (*===========================================================================*)
- (* Copy a file *)
- (* Handles commands and subroutines *)
- (* *)
- (* Copyright 1988, 1989, 1991 by H. Roy Engehausen. All rights reserved. *)
- (* *)
- (*===========================================================================*)
-
- {$O+}
-
- UNIT BBCOPY;
-
- INTERFACE
-
- USES
- DOS,
- bbbug,
- bbdummy,
- bbmdata,
- bbmess,
- bbmisc5,
- bbsdata,
- bbsema2,
- bbstr,
- bbtask,
- match;
-
- PROCEDURE oper_cf(cmd_in : str_ptr);
-
- FUNCTION copy_file_binary(from_f : file_name_str; to_f : file_name_str;
- overwrite_output : BOOLEAN) : STRING;
-
- FUNCTION copy_file_ascii (from_f : file_name_str; to_f : file_name_str;
- overwrite_output : BOOLEAN) : STRING;
-
- IMPLEMENTATION
-
- (*===========================================================================*)
- (* Copy a file -- The command version *)
- (*===========================================================================*)
-
- PROCEDURE oper_cf(cmd_in : str_ptr);
-
- VAR
- cmd_string : STRING;
- from_f : file_name_str;
- to_f : file_name_str;
- word_count : BYTE;
-
- BEGIN;
-
- cmd_string := cmd_in^;
-
- (*-----------------------------------------------------------------------*)
- (* Ready the input string *)
- (*-----------------------------------------------------------------------*)
-
- upcase_str_var(cmd_string);
-
- word_count := words(cmd_string);
-
- (*-----------------------------------------------------------------------*)
- (* Check command format *)
- (*-----------------------------------------------------------------------*)
-
- IF words(cmd_string) <> 3 THEN
- BEGIN;
- IF word_count < 3 THEN
- send_message(message_not_en)
- ELSE
- send_message(message_err_wrd);
- active_tcb^.error_sw := TRUE;
- EXIT;
- END;
-
- from_f := subwordl(cmd_string, 2, SIZEOF(from_f) - 1);
- to_f := subwordl(cmd_string, 3, SIZEOF(to_f) - 1);
-
- IF match_str(to_f, printer_match) THEN
- cmd_string := copy_file_ascii(from_f, to_f, TRUE)
- ELSE
- cmd_string := copy_file_binary(from_f, to_f, FALSE);
-
- IF cmd_string <> '' THEN
- BEGIN;
- send_tnc_data_str(cmd_string + cr);
- active_tcb^.error_sw := TRUE;
- EXIT;
- END
- ELSE
- send_message(message_file_saved);
-
- END;
-
- (*===========================================================================*)
- (* Copy a file -- Straight binary *)
- (*===========================================================================*)
-
- FUNCTION copy_file_binary(from_f : file_name_str; to_f : file_name_str;
- overwrite_output : BOOLEAN) : STRING;
-
- VAR
- b : ^CHAR;
- f_f : FILE;
- i : BYTE;
- j : INTEGER;
- r : WORD;
- s : LONGINT;
- t : LONGINT;
- t_f : FILE;
-
- BEGIN;
-
- (*-----------------------------------------------------------------------*)
- (* Get ready to open the files *)
- (*-----------------------------------------------------------------------*)
-
- ASSIGN(f_f, from_f);
- ASSIGN(t_f, to_f);
-
- (*-----------------------------------------------------------------------*)
- (* Obtain the interrupt lock *)
- (*-----------------------------------------------------------------------*)
-
- get_semaphore(semaphore_interrupts, sem_exclusive, TRUE);
-
- (*-----------------------------------------------------------------------*)
- (* Open input. If it fails, tell why and exit *)
- (*-----------------------------------------------------------------------*)
-
- {$I-}
- RESET(f_f, 1);
- {$I+}
-
- r := IORESULT;
- IF r <> 0 THEN
- BEGIN;
- free_semaphore(semaphore_interrupts);
- copy_file_binary := dos_err_message(r);
- EXIT;
- END;
-
- (*-----------------------------------------------------------------------*)
- (* Check to see if output exists *)
- (*-----------------------------------------------------------------------*)
-
- IF NOT overwrite_output THEN
- BEGIN;
- {$I-}
- RESET(t_f, 1);
- {$I+}
-
- r := IORESULT;
- IF r = 0 THEN
- BEGIN;
- free_semaphore(semaphore_interrupts);
- copy_file_binary := get_message(message_file_exists);
- EXIT;
- END;
- END;
-
- (*-----------------------------------------------------------------------*)
- (* Open output *)
- (*-----------------------------------------------------------------------*)
-
- {$I-}
- REWRITE(t_f,1);
- {$I+}
- r := IORESULT;
-
- free_semaphore(semaphore_interrupts);
-
- IF r <> 0 THEN
- BEGIN;
- CLOSE(f_f);
- copy_file_binary := dos_err_message(r);
- EXIT;
- END;
-
- (*-----------------------------------------------------------------------*)
- (* Use as much memory as possible to speed things up *)
- (*-----------------------------------------------------------------------*)
-
- s := MAXAVAIL - 8192;
-
- IF s > 1024 THEN
- s := s AND $FE00;
-
- IF s > $FE00 THEN
- s := $FE00;
-
- GETMEM(b, s);
-
- (*-----------------------------------------------------------------------*)
- (* Loop until done! *)
- (*-----------------------------------------------------------------------*)
-
- i := 0;
- j := 10 - (s DIV 256);
-
- WHILE NOT EOF(f_f) DO
- BEGIN;
- BLOCKREAD (f_f, b^, s, r);
- BLOCKWRITE(t_f, b^, r);
- IF i > j THEN
- BEGIN;
- i := 0;
- task_switch;
- END
- ELSE
- INC(i);
- END;
-
- (*-----------------------------------------------------------------------*)
- (* Free buffer *)
- (*-----------------------------------------------------------------------*)
-
- FREEMEM(b, s);
-
- (*-----------------------------------------------------------------------*)
- (* Obtain the interrupt lock *)
- (*-----------------------------------------------------------------------*)
-
- get_semaphore(semaphore_interrupts, sem_exclusive, TRUE);
-
- (*-----------------------------------------------------------------------*)
- (* Save the size *)
- (*-----------------------------------------------------------------------*)
-
- io_file_size := FILESIZE(t_f);
-
- (*-----------------------------------------------------------------------*)
- (* Fix the file date/time *)
- (*-----------------------------------------------------------------------*)
-
- GETFTIME(f_f, t);
- SETFTIME(t_f, t);
-
- (*-----------------------------------------------------------------------*)
- (* Close things up *)
- (*-----------------------------------------------------------------------*)
-
- CLOSE(f_f);
- CLOSE(t_f);
-
- (*-----------------------------------------------------------------------*)
- (* Release the interrupt lock *)
- (*-----------------------------------------------------------------------*)
-
- free_semaphore(semaphore_interrupts);
-
- (*-----------------------------------------------------------------------*)
- (* Tell everything is AOK *)
- (*-----------------------------------------------------------------------*)
-
- copy_file_binary := '';
-
- END;
-
- (*===========================================================================*)
- (* Copy a file -- Ascii mode *)
- (*===========================================================================*)
-
- FUNCTION copy_file_ascii(from_f : file_name_str; to_f : file_name_str;
- overwrite_output : BOOLEAN) : STRING;
-
- VAR
- b : STRING;
- c : LONGINT;
- f_b : ^CHAR;
- f_f : TEXT;
- l : BYTE;
- m : BYTE;
- r : WORD;
- s : LONGINT;
- t : LONGINT;
- t_b : ^CHAR;
- t_f : TEXT;
-
- PROCEDURE clean_up;
- VAR
- i : BYTE;
- BEGIN;
-
- {$I-}
- CLOSE(f_f);
- i := IORESULT;
- CLOSE(t_f);
- i := IORESULT;
- {$I+}
-
- FREEMEM(f_b, s);
- FREEMEM(t_b, s);
-
- free_semaphore(semaphore_interrupts);
-
- END;
-
- BEGIN;
-
- (*-----------------------------------------------------------------------*)
- (* Switch for printers *)
- (*-----------------------------------------------------------------------*)
-
- IF match_str(to_f, printer_match) THEN
- m := 0
- ELSE
- m := 10;
-
- (*-----------------------------------------------------------------------*)
- (* Get ready to open the files *)
- (*-----------------------------------------------------------------------*)
-
- ASSIGN(f_f, from_f);
- ASSIGN(t_f, to_f);
-
- (*-----------------------------------------------------------------------*)
- (* Obtain the interrupt lock *)
- (*-----------------------------------------------------------------------*)
-
- get_semaphore(semaphore_interrupts, sem_exclusive, TRUE);
-
- (*-----------------------------------------------------------------------*)
- (* Use as much memory as possible to speed things up *)
- (*-----------------------------------------------------------------------*)
-
- s := (MAXAVAIL div 2) - 3000;
-
- IF s > 1024 THEN
- s := s AND $FE00;
-
- IF s > $FE00 THEN
- s := $FE00;
-
- GETMEM(f_b, s);
- GETMEM(t_b, s);
-
- SETTEXTBUF(f_f, f_b^, s);
- SETTEXTBUF(t_f, t_b^, s);
-
- (*-----------------------------------------------------------------------*)
- (* Open input. If it fails, tell why and exit *)
- (*-----------------------------------------------------------------------*)
-
- {$I-}
- RESET(f_f);
- {$I+}
-
- r := IORESULT;
- IF r <> 0 THEN
- BEGIN;
- clean_up;
- copy_file_ascii := dos_err_message(r);
- EXIT;
- END;
-
- (*-----------------------------------------------------------------------*)
- (* Check to see if output exists *)
- (*-----------------------------------------------------------------------*)
-
- IF NOT overwrite_output THEN
- BEGIN;
- {$I-}
- RESET(t_f);
- {$I+}
-
- r := IORESULT;
- IF r = 0 THEN
- BEGIN;
- clean_up;
- copy_file_ascii := get_message(message_file_exists);
- EXIT;
- END;
-
- END;
-
- (*-----------------------------------------------------------------------*)
- (* Open output *)
- (*-----------------------------------------------------------------------*)
-
- {$I-}
- REWRITE(t_f);
- {$I+}
- r := IORESULT;
- IF r <> 0 THEN
- BEGIN;
- clean_up;
- copy_file_ascii := dos_err_message(r);
- EXIT;
- END;
-
- (*-----------------------------------------------------------------------*)
- (* Loop until done! *)
- (*-----------------------------------------------------------------------*)
-
- c := 0;
- l := 0;
-
- WHILE NOT EOF(f_f) DO
- BEGIN;
- READ(f_f, b);
- IF EOLN(f_f) THEN
- BEGIN;
- {$I-}
- WRITELN(t_f, b);
- {$I+}
- c := c + LENGTH(b) + 1;
- READLN(f_f, b);
- END
- ELSE
- BEGIN;
- {$I-}
- WRITE(t_f, b);
- {$I+}
- c := c + LENGTH(b);
- END;
-
- r := IORESULT;
- IF r <> 0 THEN
- BEGIN;
- clean_up;
- copy_file_ascii := dos_err_message(r);
- EXIT;
- END;
-
- IF l > m THEN
- BEGIN;
- l := 0;
- task_switch;
- END;
-
- END;
-
- (*-----------------------------------------------------------------------*)
- (* Save the size *)
- (*-----------------------------------------------------------------------*)
-
- io_file_size := c;
-
- (*-----------------------------------------------------------------------*)
- (* Fix the file date/time *)
- (*-----------------------------------------------------------------------*)
-
- GETFTIME(f_f, t);
- SETFTIME(t_f, t);
-
- (*-----------------------------------------------------------------------*)
- (* Close things up *)
- (*-----------------------------------------------------------------------*)
-
- clean_up;
-
- (*-----------------------------------------------------------------------*)
- (* Tell everything is AOK *)
- (*-----------------------------------------------------------------------*)
-
- copy_file_ascii := '';
-
- END;
-
- END.